package com.salesmanager.web.admin.controller.product;

import com.salesmanager.core.business.generic.exception.ServiceException;
import com.salesmanager.core.business.product.Product;
import com.salesmanager.core.business.product.service.ProductService;
import com.salesmanager.core.business.statistics.model.ProductCriteria;
import com.salesmanager.core.utils.MD5Util;
import com.salesmanager.web.admin.controller.AbstractController;
import com.salesmanager.web.admin.controller.ControllerConstants;
import com.salesmanager.web.admin.entity.catalog.ProductConfiguration;
import com.salesmanager.web.entity.app.AppChannelAttribute;
import com.salesmanager.web.entity.product.ProductAttribute;
import com.salesmanager.web.model.paging.PaginationDataList;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

/**
 * Created by LiuTong on 2017/11/21.
 */
@Controller
public class ProductController extends AbstractController {

    @Autowired
    private ProductService productService;

    @RequestMapping("/product/productList")
    public String productList(@ModelAttribute("productAttribute") ProductAttribute productAttribute, Model model, HttpServletRequest request, HttpServletResponse response) {
        super.setMenu(model, request, Arrays.asList("product-manage", "product-manage"), "product-manage");
        ProductCriteria criteria = new ProductCriteria();
        //分页
        criteria.setMaxCount(productAttribute.getPageSize());
        criteria.setStartIndex(productAttribute.getOffset() - 1);
        //编码
        if (!StringUtils.isBlank(productAttribute.getProductCode())) {
            criteria.setProductCode(productAttribute.getProductCode());
        }
        //名称
        if (!StringUtils.isBlank(productAttribute.getProductName())) {
            criteria.setProductName(productAttribute.getProductName());
        }

        //id
        if (productAttribute.getProductId() != null) {
            criteria.setProductId(productAttribute.getProductId());
        }
        PaginationDataList<Product> dataList = productService.getProductList(criteria);
        model.addAttribute("dataList", dataList);
        return ControllerConstants.Tiles.Product.productList;
    }


    @RequestMapping("/product/addProductPage")
    public String addProductPage(Model model, HttpServletRequest request, HttpServletResponse response) {
        super.setMenu(model, request, Arrays.asList("product-manage", "product-manage"), "product-manage");
        String productId = request.getParameter("productId");
        ProductConfiguration configuration = new ProductConfiguration();
        Product product = null;
        if (!StringUtils.isBlank(productId)) {
            product = productService.getById(Long.parseLong(productId));
            configuration.setProduct(product);
        } else {
            product = new Product();
            configuration.setProduct(product);
        }
        model.addAttribute("configuration", configuration);
        model.addAttribute("productId", productId);
        return ControllerConstants.Tiles.Product.productEdit;
    }

    @RequestMapping("/product/saveProduct")
    public String saveProduct(@ModelAttribute("configuration") ProductConfiguration configuration, Model model, HttpServletRequest request, HttpServletResponse response) throws IOException, ServiceException {
        super.setMenu(model, request, Arrays.asList("product-manage", "product-manage"), "product-manage");
        Product product = configuration.getProduct();
        if (product.getId() != null) {
            product = productService.getById(product.getId());
            product.setProductCode(configuration.getProduct().getProductCode());
            product.setCount(configuration.getProduct().getCount());
            product.setProductName(configuration.getProduct().getProductName());
            product.setEndTime(configuration.getProduct().getEndTime());
            product.setStartTime(configuration.getProduct().getStartTime());
            product.setProductDesc(configuration.getProduct().getProductDesc());
        }
        if (configuration.getFirstImage() != null && !configuration.getFirstImage().isEmpty()) {
            MultipartFile file = configuration.getFirstImage();
            String fileName = file.getOriginalFilename();
            product.setImagInputStream(configuration.getFirstImage().getInputStream());
            product.setImagName(fileName);
        }
        productService.saveOrUpdate(product);
        return "redirect:/product/productList";
    }
}
